<?php

/*
 *  Action	$2
 *  
 *  +$2
 *  Each player (including you) looks at the top card of his deck,
 *  and discards it or puts it back.
 * 
 *  In games using this, when you gain a Duchy, you may gain a Duchess
 * 
 * TODO: fix this later
 */

class Duchess extends Card implements iAction, iKingdom, iHinterlands, iReactingCard {

    private $reactConditions;
    private $duchyKey;

    public function __construct() {
        parent::__construct("Duchess", 2);

        $this->reactConditions = new ReactionConditions();
        $this->reactConditions->action = array(Gain);
        $this->reactConditions->actor = array(Yourself);
        $this->reactConditions->cardLocation = array(BoardCards);
        $this->reactConditions->limit = OncePerReaction;
        $this->reactConditions->isMandatory = true;

        $duchy = new Duchy();
        $this->duchyKey = $duchy->getKey();
    }

    public function getDescription() {
        return array(
            "+$2",
            "Each player (including you) looks at the top card of his deck, and discards it or puts it back.",
            "In games using this, when you gain a Duchy, you may gain a Duchess");
    }

    public function play(Game $game) {
        $game->addMoney(new Price(2));
        ScreenDisplay::addScreenMessage($game, true, "get +2$", "gets +2$");

        $game->setPhase(Phase_Sleep);

        $options = array(new Option(1, "Yes"), new Option(2, "No"));

        $names = $game->getPlayersNames();
        foreach ($names as $name) {
            $card = $game->peekCardFromDrawDeck($name);
            if ($card) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Discard " . $card->getValue() . " from top of draw pile?", $name, true);
            }
        }

        // the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {
            $card = $game->peekCardFromDrawDeck($name);
            if ($output[0] == "Yes") {
                $cards = $game->discardCardsFromDrawDeck(1, $name);
                ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($cards), "discards " . sizeof($cards) . " cards from top of his deck", $name);
            } else {
                ScreenDisplay::addScreenMessage($game, true, " leave " . nicePrint($card) . " on top of your deck", "leaves " . sizeof($card) . " cards on  top of his deck", $name);
            }
            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
    }

    public function getConditions() {
        return $this->reactConditions;
    }

    public function isReactRelevant(Game $game, $card, $name) {
        if ($card->getKey() == $this->duchyKey) {
            return true;
        }
        return false;
    }

    public function react(Game $game, $card, $name) {

        if ($card->getKey() == $this->duchyKey) {
            $options = array(new Option(1, "Yes"), new Option(2, "No"));
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Would you like to gain a duchess?");
            if ($output[0] == "Yes") {
                $game->moveCardFromPileToPile($this, BoardCards, DiscardPile, $name);
                ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($this), "gains " . ScreenDisplay::$LMSG, $name);
            }
            $game->setTempCard($card);
        }
        return true;
    }

}

?>